package fi.pakkanen.yarpg.item;

import java.util.Set;

import org.apache.log4j.Logger;

/**
 * Implementation of basic item behaviour.
 * 
 * @author Oliver Michels <oliver.michels@gmx.net>
 * 
 */
public abstract class AbstractItem implements Item {

	/** Logging */
	Logger logger = Logger.getLogger(getClass());

	/** The current state of this item */
	private ItemState state;

	/**
	 * Create a new item with the given start state.
	 * 
	 * @param startState
	 */
	public AbstractItem(final ItemState startState) {
		state = startState;
	}

	/**
	 * Get a set with all valid states for the item.
	 * 
	 * @return a set with valid states
	 */
	protected abstract Set<ItemState> getValidStates();

	@Override
	public boolean isState(final ItemState stateToCheck) {
		return state == stateToCheck;
	}

	/**
	 * Get the current state.
	 * 
	 * @return the current state
	 */
	protected ItemState getState() {
		return state;
	}

	@Override
	public void setState(final ItemState newState) {
		if (getValidStates().contains(newState)) {
			state = newState;
		} else {
			logger.warn("Unable to set state to " + newState + " for " + this);
			throw new IllegalStateException("State " + newState + " not legal for " + this);
		}
	}

	@Override
	public boolean changeState() {
		return state.changeState(this);
	}

	@Override
	public boolean isConsumable() {
		return false;
	}

	@Override
	public boolean isEquippable() {
		return false;
	}

	@Override
	public boolean isTriggerable() {
		return false;
	}

}
